<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:param name="tableName"/>
  <xsl:template match="/">
    <xsl:for-each select="CogDataSet/TableInfo[Table = $tableName]">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using <xsl:value-of select="Namespace"/>.Interface;

namespace <xsl:value-of select="Namespace"/>
{
    public partial class <xsl:value-of select="ClassPrefix"/>DataSet
    {
        public partial class <xsl:value-of select="ManagedTable"/>DataTable : IDataTable
        {
            public override void EndInit()
            {
                ColumnChanged += (sender, args) => ((<xsl:value-of select="ManagedTable"/>Row)args.Row).OnPropertyChanged(args);
            }
      <xsl:for-each select="ColumnInfo[IsPrimaryKey = 'true' or IsIndexed = 'true' or IsForeignKey = 'true']">
            public IEnumerable&lt;<xsl:value-of select="Table2"/>Row&gt; LoadBy<xsl:value-of select="Column"/>(<xsl:value-of select="ManagedDataType"/><xsl:text> </xsl:text><xsl:value-of select="ManagedColumn"/>)
            {
                return ExecuteQuery&lt;<xsl:value-of select="Table2"/>Row&gt;(this, "<xsl:value-of select="../Schema"/>.<xsl:value-of select="ManagedTable"/>_LoadBy<xsl:value-of select="Column"/>",
                    new Dictionary&lt;string, object&gt; {{"@<xsl:value-of select="Column"/>", <xsl:value-of select="ManagedColumn"/>}});
            }
          </xsl:for-each>
            public void Save()
            {
                SaveChanges(this);
            }

            #region IDataTable Members

            public string SelectStoredProc
            {
                get { 
                      return "SELECT <xsl:for-each select="ColumnInfo[Table = $tableName]">[<xsl:value-of select="Column"/>]<xsl:if test="not(position()=last())">," +
                             "</xsl:if></xsl:for-each>" +
                             "FROM <xsl:value-of select="Schema"/>.<xsl:value-of select="Table"/>_Select"; 
                    }
            }

            public string InsertStoredProc
            {
                get { return "<xsl:value-of select="Schema"/>.<xsl:value-of select="ManagedTable"/>_Insert"; }
            }

            public string UpdateStoredProc
            {
                get { return "<xsl:value-of select="Schema"/>.<xsl:value-of select="ManagedTable"/>_Update"; }
            }

            public string DeleteStoredProc
            {
                get { return "<xsl:value-of select="Schema"/>.<xsl:value-of select="ManagedTable"/>_Delete"; }
            }

            #endregion
        }
        
        partial class <xsl:value-of select="ManagedTable"/>Row : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(DataColumnChangeEventArgs args)
            {
                var handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(args.Column.ColumnName));
            }
        }
    }
}
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
